package itheima.com.SocketTCP;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketClienmt {

    public static void main(String[] args) throws IOException {

        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("客户端发送内容".getBytes());
        System.out.print("客户端发送内容");

        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write("hello word字符流");
        bufferedWriter.newLine();
        bufferedWriter.flush();//如果使用的字符流，需要手动刷新，否则数据不会写入数据通道
        socket.shutdownOutput();

        byte[] buf = new byte[1024];
        int len = 0;
        InputStream inputStream = socket.getInputStream();
        while ((len = inputStream.read(buf)) != -1) {
            System.out.println(new String(buf, 0, len));
        }

        socket.shutdownOutput();

        socket.close();
        outputStream.close();



    }

}
